package me.zhengjie.modules.system.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import me.zhengjie.modules.system.domain.ExamReportParams;
import me.zhengjie.modules.system.domain.vo.ExamParamsVo;
import me.zhengjie.modules.system.service.ExamReportParamsService;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author CN-xianchengzhao
 * @description: TODO
 * @date 2021/11/21 12:14
 */
@Component
public class UploadExamParamsListener implements ReadListener<ExamParamsVo> {

    public static ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

    @Autowired
    ExamReportParamsService examReportParamsService;

    @Override
    public void invoke(ExamParamsVo examParamsVo, AnalysisContext analysisContext) {
        if (StringUtils.isBlank(examParamsVo.getParamName())) {
            return;
        }
        ExamReportParams examReportParams = new ExamReportParams();
        BeanUtils.copyProperties(examParamsVo, examReportParams);

        examReportParams.setParamType(threadLocal.get());
        examReportParams.setParamId("b_"+examReportParams.getParamType()+"_"+examParamsVo.getParamName());
        examReportParamsService.create(examReportParams);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

}
